﻿#region Copyright
/*
Copyright (c) 2008 EPiServer AB.  All rights reserved.

This code is released by EPiServer AB under the Source Code File - Specific License Conditions, published August 20, 2007. 
See http://www.episerver.com/Specific_License_Conditions for details. 
*/
#endregion

#region Known Limitations
/*
The template has no known limitations.
*/
#endregion

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.Common.Tags;
using EPiServer.Core;
using EPiServer.Common.Data;
using EPiServer.Research.Wiki.Core;

namespace EPiServer.Templates.RelatePlus.Templates.WikiX.Core
{
    /// <summary>
    /// Class that renders all the tags for an entity in the form of links
    /// </summary>
    [ToolboxData("<{0}:LinkedTags runat=server></{0}:LinkedTags>")]
    public class LinkedTags : WebControl
    {
        #region Member variables
        /// <summary>
        /// The entity to write tags for
        /// </summary>
        private ITaggableEntity _entity;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the entity.
        /// </summary>
        /// <value>The entity.</value>
        public ITaggableEntity Entity
        {
            get
            {
                if (_entity == null && ViewState["entityId"] != null && ViewState["entityType"] != null)
                {
                    Type t = Type.GetType((string)ViewState["entityType"]);
                    IEntityProvider provider = EntityProviderHandler.GetEntityProvider(t);

                    if (provider != null)
                    {
                        _entity = (ITaggableEntity)provider.GetEntityInstance(t, Convert.ToInt32(ViewState["entityId"]));
                    }
                }

                return _entity;
            }
            set
            {
                _entity = value;

                if (value != null)
                {
                    ViewState["entityId"] = value.ID;
                    ViewState["entityType"] = FrameworkFactoryBase.GetTypeName(value.GetType());
                }
                else
                {
                    ViewState.Clear();
                }
            }
        }
        #endregion

        #region Method overrides
        /// <summary>
        /// Renders the all tags of the entity in the form of links to the specified HTML writer.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (Entity != null)
            {
                if (Entity.Tags.Count > 0)
                {
                    writer.Write(String.Format("{0}&nbsp;{1}", LanguageManager.Instance.Translate("/general/tags"), " "));
                }
                for (int i = 0; i < Entity.Tags.Count; i++)
                {
                    // Render A tag
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, Entity.Tags[i].GetTagsUri().ToString());
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, Entity.Tags[i].Name);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    writer.Write(Entity.Tags[i].Name.FormatContentText());

                    // End the A tag
                    writer.RenderEndTag();

                    // Add a comma if it not the last one
                    if (i != Entity.Tags.Count - 1)
                    {
                        writer.Write(", ");
                    }
                }
            }
        }
        #endregion
    }
}